package com.smartalarmclock.UI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IProfile;

public class ProfileActivity extends Activity {
	private ArrayList<IProfile> m_ActiveProfiles = new ArrayList<IProfile>();
	private List<ProfileItemDetails> m_ProfilesItemDetailsList = new ArrayList<ProfileItemDetails>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_screen);
        
        // set listener for buttons
        final Button backButton = (Button)findViewById(R.id.button_profile_back);
        backButton.setOnClickListener(backButtonClickListener);
        final Button newProfileButton = (Button)findViewById(R.id.button_profile_new);
        newProfileButton.setOnClickListener(newProfileButtonClickListener);
        
        // update the active profiles list from the AcManager
        updateActiveProfilesListItems();
        
        // set profile list adapter and items on-click listener
        final ListView profileListView = (ListView)findViewById(R.id.listView_profile_list);
        profileListView.setAdapter(new ProfileArrayAdapter(this, m_ProfilesItemDetailsList));
        profileListView.setOnItemClickListener(profileListItemClickListener);
    }
    
    private void updateActiveProfilesListItems()
    {
    	// clear both lists
    	m_ActiveProfiles.clear();
    	m_ProfilesItemDetailsList.clear();
    	
    	// get active profiles from AcManager
		// ToDo: fix stubs...
    	m_ActiveProfiles = AcManager.GetInstance().GetActiveProfiles(this);
		
		// build a list with all active profiles list items
		for (IProfile activeProfile : m_ActiveProfiles)
		{
			m_ProfilesItemDetailsList.add(new ProfileItemDetails(activeProfile));
		}
    }
    
    private OnItemClickListener profileListItemClickListener = new OnItemClickListener() {
     	private static final String TAG = "OnItemClickListener";

        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
    	    Log.v(TAG, "Clicked on profile (Ofer)");

        	final IProfile item = ((ProfileItemDetails)parent.getItemAtPosition(position)).getProfile();
          
        	// start profile details activity for editing a profile 
      		Intent newProfileActivityIntent = new Intent(ProfileActivity.this, CreateNewProfileActivity.class);
      		newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_IS_NEW, false);
      		newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, item.GetId());
      		startActivity(newProfileActivityIntent);
      		
    	    Log.v(TAG, "Returned from editing existing profile activity (Ofer)");

      		// update the active profiles from the AcManager
      		updateActiveProfilesListItems();
      		final ListView profileListView = (ListView)findViewById(R.id.listView_profile_list);
      		Log.v(TAG, "Right before getting the selected arrayAdapter (Ofer)");
            ArrayAdapter<ProfileItemDetails> arrayAdapter = (ArrayAdapter<ProfileItemDetails>)profileListView.getAdapter();
      		Log.v(TAG, "Right After getting the selected arrayAdapter (Ofer)");

			arrayAdapter.notifyDataSetChanged();
			
      		Log.v(TAG, "Activated notify.DataSetChanged on arrayAdapter (Ofer)");
        }
    };
    
    private OnClickListener newProfileButtonClickListener = new OnClickListener()
    {
    	@Override
        public void onClick(View v)
        {   
        	// start profile details activity for a new profile 
        	Intent newProfileActivityIntent = new Intent(ProfileActivity.this, CreateNewProfileActivity.class);
        	newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_IS_NEW, true);
        	newProfileActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, -1);
        	startActivity(newProfileActivityIntent);
        	
        	// update the active profiles from the AcManager
      		updateActiveProfilesListItems();
      		final ListView profileListView = (ListView)findViewById(R.id.listView_profile_list);
            ArrayAdapter<ProfileItemDetails> arrayAdapter = (ArrayAdapter<ProfileItemDetails>)profileListView.getAdapter();
			arrayAdapter.notifyDataSetChanged();
        }
    };
    
    @Override
    protected void onResume()
    {
    	super.onResume();
    	// update the active profiles from the AcManager
  		updateActiveProfilesListItems();
  		final ListView profileListView = (ListView)findViewById(R.id.listView_profile_list);
        ArrayAdapter<ProfileItemDetails> arrayAdapter = (ArrayAdapter<ProfileItemDetails>)profileListView.getAdapter();
		arrayAdapter.notifyDataSetChanged();
    }
    
    private OnClickListener backButtonClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {   
             finish();
        }
    };
    
    private class ProfileArrayAdapter extends ArrayAdapter<ProfileItemDetails>
    {
    	private LayoutInflater l_Inflater;
    	HashMap<IProfile, Integer> m_IdMap = new HashMap<IProfile, Integer>();
    	
    	public ProfileArrayAdapter(Context context, List<ProfileItemDetails> activeProfileItems)
    	{
    		super(context, R.layout.profile_list_item, activeProfileItems);
    		
    		l_Inflater = LayoutInflater.from(context);
    		
    		// set profiles in id-map
    		int i = 0;
    		for (ProfileItemDetails profileItem : activeProfileItems)
    		{
    			m_IdMap.put(profileItem.getProfile(), i++);
    		}
    	}
    	
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder holder;
    		if (convertView == null) {
    			convertView = l_Inflater.inflate(R.layout.profile_list_item, null);
    			holder = new ViewHolder();
    			holder.txt_itemName = (TextView)convertView.findViewById(R.id.profile_list_item_text);
    			holder.itemImage = (ImageView)convertView.findViewById(R.id.profile_list_item_image);

    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		
    		holder.txt_itemName.setText(getItem(position).getName());
    		holder.itemImage.setImageResource(R.drawable.profile);

    		return convertView;
    	}
    	
    	@Override
    	public long getItemId(int position)
    	{
    		ProfileItemDetails item = getItem(position);
    		return m_IdMap.get(item.getProfile());
    	}
    	
    	@Override
    	public boolean hasStableIds()
    	{
    		return true;
    	}
    }
    
    static class ViewHolder
    {
		TextView txt_itemName;
		ImageView itemImage;
	}
    
    public class ProfileItemDetails
    {
    	private IProfile m_Profile = null;
    	
    	public ProfileItemDetails(IProfile i_Profile)
    	{
    		m_Profile = i_Profile;
    	}
    	
    	public String getName()
    	{
    		return m_Profile.GetName();
    	}
    	
    	public IProfile getProfile()
    	{
    		return m_Profile;
    	}
    }
}
